﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using AncientLand.Service.Entity;
using AncientLand.Service.IBiz;

namespace AncientLand.Controllers.Admin
{
   public class NewsAdminController: Controller
    {
       private readonly INewsBiz _newService;

        public NewsAdminController(INewsBiz newsService)
        {
            this._newService = newsService;
        }


       [Authorize]
       public ActionResult List(int? page = 1,int? pageSize=10)
       {
           ViewBag.Model = "News";

           var size = Convert.ToInt16(System.Configuration.ConfigurationSettings.AppSettings["AdminpageSize"]); 

           var totalRecord = 0;
           var news = _newService.GetNewsByPages(out totalRecord, pageIndex: page.GetValueOrDefault(), pageSize: size, orderFiled: "");

           var item = new PageItem()
           {
               CurrentPage = page.GetValueOrDefault(),
               TotalRecords = totalRecord,
               TotalPages = (totalRecord % size == 0) ? totalRecord / size : totalRecord / size + 1,
               ControllerName = "NewsAdmin",
               ActionName = "List"
           };

           var viewModels = new Pager<News>() { DataSource = news, Item = item };

           return View(viewModels);
       }

       [Authorize]
       public ActionResult Edit(int? id=1)
       {
           ViewBag.Model = "News";

           var newsItem = _newService.GetById(id.GetValueOrDefault());
           return View(newsItem);
       }

       [Authorize]
       [HttpPost]
       [ValidateInput(false)]
       public RedirectToRouteResult Edit(News news)
       {
           if (ModelState.IsValid)
           {
               news.CreatedTime = DateTime.Now;
               _newService.Update(news);
           }
           return RedirectToAction("List");
       }

       [Authorize]
       public ActionResult Add()
       {
           ViewBag.Model = "News";
           return View();
       }

       [Authorize]
       [HttpPost]
       [ValidateInput(false)]
       public RedirectToRouteResult Add(News news)
       {
           if (ModelState.IsValid)
           {
               news.CreatedTime = DateTime.Now;
               news.ReadTimes = 1;
               _newService.Create(news);
           }
           return RedirectToAction("List");
       }

       [Authorize]
       public ActionResult Delete(int id)
       {
           _newService.Delete(id);
           return RedirectToAction("List");
       }

       public JsonResult GetNews(DataTableParameter param)
       {
           return Json(new{
               sEcho = param.sEcho,
                    iTotalRecords = 50,
                    iTotalDisplayRecords = 50,
                    aaData = new List<object> {
                                 new string[] {"1","公司信息","地址信息","城市信息"},
                                 new string[] {"1","公司信息","地址信息","城市信息"},
                                 new string[] {"1","公司信息","地址信息","城市信息"},
                                 new string[] {"1","公司信息","地址信息","城市信息"},
                                 new string[] {"1","公司信息","地址信息","城市信息"},
                                 new string[] {"1","公司信息","地址信息","城市信息"},
                                 new string[] {"1","公司信息","地址信息","城市信息"},
                                 new string[] {"1","公司信息","地址信息","城市信息"},
                                 new string[] {"1","公司信息","地址信息","城市信息"},
                                 new string[]{"1","公司信息","地址信息","城市信息"}
                                   }
                }, JsonRequestBehavior.AllowGet);
       }

      
      
    }

   public class DataTableParameter
   {
       /// <summary>
       /// DataTable请求服务器端次数
       /// </summary>       
       public string sEcho { get; set; }

       /// <summary>
       /// 过滤文本
       /// </summary>
       public string sSearch { get; set; }

       /// <summary>
       /// 每页显示的数量
       /// </summary>
       public int iDisplayLength { get; set; }

       /// <summary>
       /// 分页时每页跨度数量
       /// </summary>
       public int iDisplayStart { get; set; }

       /// <summary>
       /// 列数
       /// </summary>
       public int iColumns { get; set; }

       /// <summary>
       /// 排序列的数量
       /// </summary>
       public int iSortingCols { get; set; }

       /// <summary>
       /// 逗号分割所有的列
       /// </summary>
       public string sColumns { get; set; }
   }
}
